package com.atguigu.alice;


import org.junit.Test;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;

public class PipeDemo {

    @Test
    public void testPipe() throws IOException {
        Pipe pipe = Pipe.open();
        Pipe.SinkChannel sinkChannel = pipe.sink();
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.put("atguigu".getBytes());
        byteBuffer.flip();

        sinkChannel.write(byteBuffer);
        Pipe.SourceChannel sourceChannel = pipe.source();
        ByteBuffer allocate = ByteBuffer.allocate(1024);
        int length = sourceChannel.read(allocate);
        System.out.println(new String(allocate.array(),0,length));

        sourceChannel.close();
        sinkChannel.close();
    }
}
